/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: server.c
> Author:cjy
> Mail: 
> Created Time: 2024年08月13日 星期2 16时19分12秒
***************************************************/
//基于基于udp协议开发一个简易时间服务器。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
char* str_ch_return_ch_in_strpoint(char *arr1,char ch,int n){
    int count = 0;
    char*str = arr1;
    int i = 0;
    while(*(str+i)!='\0')
    {
        if(*(str + i) == ch){
            count++;
            if(count == n) {
                break;
            }
        }
        i++;
    }
    return str+i;
}
char* get_arry(char*arr1,char*arr2,char*start,char*end) {
    int i = 0,j = 0;
    while(*(arr1 + i) != '\0') {
        if(arr1+i > start && arr1+i < end) {
            *(arr2+j) = *(arr1 + i);
            j++;
        }
        i++;
    }
    return arr2;
}
int main(int argc,const char* argv[])
{
	int sock = 0;
	int ret = 0;
	char buf[1024] = {0};
	char dest[1024] = {0};

	struct sockaddr_in addr;
        memset(&addr,0,sizeof(addr));

	sock = socket(AF_INET,SOCK_DGRAM,0);
	if(sock == -1)
	{
		perror("sock");
		exit(-1);
	}

	addr.sin_family = AF_INET;
        addr.sin_port = htons(6666);
        addr.sin_addr.s_addr = inet_addr("192.168.198.128");

	ret = bind(sock,(struct sockaddr *)&addr,sizeof(struct sockaddr));
	if(ret == -1)
	{
		perror("bind");
		exit(-1);
	}

	struct sockaddr_in cliaddr;
	memset(&cliaddr,0,sizeof(cliaddr));

	int len = sizeof(struct sockaddr);

	char *p1 = NULL;
	char *p2 = NULL;
	char arr[10] = {0};
	char ch = '$';
	while(1)
	{
		ret = recvfrom(sock,buf,sizeof(buf),0,(struct sockaddr *)&cliaddr,&len);
		if(ret == -1)
		{
			perror("recvfrom");
			exit(-1);
		}
		printf("%s\n",buf);
		p1 = str_ch_return_ch_in_strpoint(buf,ch,2);
		p2 = str_ch_return_ch_in_strpoint(buf,ch,3);
		get_arry(buf,arr,p1,p2);
		int num1 = 0;
		int num2 = 0;
		int sum = 0;
		float num3 = 0;
		float num4 = 0;
		float sum1 = 0;
		if(strcmp(arr,"add") == 0){
			memset(&arr,0,sizeof(arr));
			p1 = str_ch_return_ch_in_strpoint(buf,ch,3);
			p2 = str_ch_return_ch_in_strpoint(buf,ch,4);
			int num1 = (int)get_arry(buf,arr,p1,p2)[1];
			num1 = atoi(arr);
			p1 = str_ch_return_ch_in_strpoint(buf,ch,4);
			p2 = str_ch_return_ch_in_strpoint(buf,ch,5);
			get_arry(buf,arr,p1,p2);
			num2 = atoi(arr);
			sum = num1 + num2;
			printf("%d\n",sum);
			
			ret = sendto(sock,&sum,sizeof(sum),0,(struct sockaddr *)&cliaddr,sizeof(struct sockaddr));
			memset(&sum,0,sizeof(sum));
			
		}
		else if(strcmp(arr,"jian") == 0){
			memset(&arr,0,sizeof(arr));
			p1 = str_ch_return_ch_in_strpoint(buf,ch,3);
			p2 = str_ch_return_ch_in_strpoint(buf,ch,4);
			int num1 = (int)get_arry(buf,arr,p1,p2)[1];
			num1 = atoi(arr);
			p1 = str_ch_return_ch_in_strpoint(buf,ch,4);
			p2 = str_ch_return_ch_in_strpoint(buf,ch,5);
			get_arry(buf,arr,p1,p2);
			num2 = atoi(arr);
			sum = num1 - num2;
			printf("%d\n",sum);
			
			ret = sendto(sock,&sum,sizeof(sum),0,(struct sockaddr *)&cliaddr,sizeof(struct sockaddr));
			memset(&sum,0,sizeof(sum));
			
		}
		else if(strcmp(arr,"cheng") == 0){
			memset(&arr,0,sizeof(arr));
			p1 = str_ch_return_ch_in_strpoint(buf,ch,3);
			p2 = str_ch_return_ch_in_strpoint(buf,ch,4);
			int num1 = (int)get_arry(buf,arr,p1,p2)[1];
			num1 = atoi(arr);
			p1 = str_ch_return_ch_in_strpoint(buf,ch,4);
			p2 = str_ch_return_ch_in_strpoint(buf,ch,5);
			get_arry(buf,arr,p1,p2);
			num2 = atoi(arr);
			sum = num1 * num2;
			printf("%d\n",sum);
			
			ret = sendto(sock,&sum,sizeof(sum),0,(struct sockaddr *)&cliaddr,sizeof(struct sockaddr));
			memset(&sum,0,sizeof(sum));
			
		}
		
		else if(strcmp(arr,"chu") == 0){
			memset(&arr,0,sizeof(arr));
			p1 = str_ch_return_ch_in_strpoint(buf,ch,3);
			p2 = str_ch_return_ch_in_strpoint(buf,ch,4);
			int num1 = (int)get_arry(buf,arr,p1,p2)[1];
			num3 = atof(arr);
			p1 = str_ch_return_ch_in_strpoint(buf,ch,4);
			p2 = str_ch_return_ch_in_strpoint(buf,ch,5);
			get_arry(buf,arr,p1,p2);
			num4 = atof(arr);
			sum1 = num3 / num4;
			printf("%f\n",sum1);
			
			ret = sendto(sock,&sum1,sizeof(sum1),0,(struct sockaddr *)&cliaddr,sizeof(struct sockaddr));
			memset(&sum1,0,sizeof(sum1));
			
		}
		
		else if(strcmp(buf,"exit") == 0){
			break;
}
	}

	close(sock);


    return 0;
}
